﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using System.Configuration;
using Estimate.Common;
using Estimate.Common.Repository;
using Estimate.Common.DBHealth;
using Estimate.Common.DBFieldTypes;
using ArchiveSystem.ShowBusy;

namespace ManualTools
{
    public partial class ManualToolsForm : Form
    {
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        extern static IntPtr GetForegroundWindow();
        public  IntPtr hWnd = GetForegroundWindow();// 当前windows窗口句柄
        MyThread myThread = new MyThread();
   
        public ManualToolsForm()
        {
            InitializeComponent();
        }

        private void ManualToolsForm_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;

            OperateTools.txtLog = this.txtLog;

            dtpBeginDate_ImportData.Value = DateTime.Today;
            dtpEndDate_ImportData.Value = DateTime.Today;

            cbbBeginYear_ImportSchedule.SelectedIndex = cbbBeginYear_ImportSchedule.FindString(DateTime.Today.Year.ToString());
            cbbBeginMonth_ImportSchedule.SelectedIndex = cbbBeginMonth_ImportSchedule.FindString(DateTime.Today.Month.ToString());
            cbbEndYear_ImportSchedule.SelectedIndex = cbbEndYear_ImportSchedule.FindString(DateTime.Today.Year.ToString());
            cbbEndMonth_ImportSchedule.SelectedIndex = cbbEndMonth_ImportSchedule.FindString(DateTime.Today.Month.ToString());

            cbbBeginYear_CheckSchedule.SelectedIndex = cbbBeginYear_CheckSchedule.FindString(DateTime.Today.Year.ToString());
            cbbBeginMonth_CheckSchedule.SelectedIndex = cbbBeginMonth_CheckSchedule.FindString(DateTime.Today.Month.ToString());
            cbbEndYear_CheckSchedule.SelectedIndex = cbbEndYear_CheckSchedule.FindString(DateTime.Today.Year.ToString());
            cbbEndMonth_CheckSchedule.SelectedIndex = cbbEndMonth_CheckSchedule.FindString(DateTime.Today.Month.ToString());

            dtpBeginDate_CheckForeData.Value = DateTime.Today;
            dtpEndDate_CheckForeData.Value = DateTime.Today;

            cbbBeginYear_ImportManualData.SelectedIndex = cbbBeginYear_ImportManualData.FindString(DateTime.Today.Year.ToString());
            cbbBeginMonth_ImportManualData.SelectedIndex = cbbBeginMonth_ImportManualData.FindString(DateTime.Today.Month.ToString());
            cbbEndYear_ImportManualData.SelectedIndex = cbbEndYear_ImportManualData.FindString(DateTime.Today.Year.ToString());
            cbbEndMonth_ImportManualData.SelectedIndex = cbbEndMonth_ImportManualData.FindString(DateTime.Today.Month.ToString());

            dtpBeginDate_ImportRealData.Value = DateTime.Today;
            dtpEndDate_ImportRealData.Value = DateTime.Today;

            dtpBeginDate_CopyData.Value = DateTime.Today;
            dtpEndDate_CopyData.Value = DateTime.Today;

            dtpBeginDate_ReImportData.Value = DateTime.Today;
            dtpEndDate_ReImportData.Value = DateTime.Today;

            dtpBeginDate_ReSemi.Value = DateTime.Today;
            dtpEndDate_ReSemi.Value = DateTime.Today;

            cbbBeginYear_BuildData.SelectedIndex = cbbBeginYear_BuildData.FindString(DateTime.Today.Year.ToString());
            cbbBeginMonth_BuildData.SelectedIndex = cbbBeginMonth_BuildData.FindString(DateTime.Today.Month.ToString());
            cbbEndYear_BuildData.SelectedIndex = cbbEndYear_BuildData.FindString(DateTime.Today.Year.ToString());
            cbbEndMonth_BuildData.SelectedIndex = cbbEndMonth_BuildData.FindString(DateTime.Today.Month.ToString());

            dtpBeginDate_ExportData.Value = DateTime.Today;
            dtpEndDate_ExportData.Value = DateTime.Today;
            dtpEndDate_ExportData.Enabled = false;

            DataTable dt = new DataTable();
            dt.Columns.Add("checked");
            dt.Columns.Add("description");
            dt.Columns.Add("table");
            dt.Columns.Add("dir");
            dt.Columns.Add("filter");
            dgvForeData.DataSource = new BindingSource(dt, null);
            XmlDocument doc = new XmlDocument();
            doc.Load(Application.StartupPath + @"\ManualTools.exe.config");
            XmlNode node = doc.SelectSingleNode("//ForeDataFiles");
            string dir = node.Attributes["Dir"].Value;
            foreach (XmlNode n in node.ChildNodes)
            {
                string tableName = n.Attributes["TableName"].Value;
                string dirPath = n.Attributes["FileDir"].Value;
                string description = n.Attributes["Description"].Value;
                string fileFilter = n.Attributes["FileFilter"].Value;
                dt.Rows.Add(new object[] { 0, description, tableName, dir + "\\" + dirPath, fileFilter });
                //importForeData(dbConnectionString, dt, tableName, dirPath, fileFilter, description);
            }
        }

        private void rbImportOneDayData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ImportData.Enabled = false;
        }

        private void rbImportSeveralDaysData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ImportData.Enabled = true;
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确定导入数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(ImportRealData);
            myThread.Start();

            //ImportRealData();
        }
        private void ImportForeData()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            if (rbImportOneDayData.Checked)
                OperateTools.ImportData(dtpBeginDate_ImportData.Value);
            else if (rbImportSeveralDaysData.Checked)
            {
                if (dtpBeginDate_ImportData.Value > dtpEndDate_ImportData.Value)
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                DateTime cur = dtpBeginDate_ImportData.Value;
                while (cur <= dtpEndDate_ImportData.Value)
                {
                    OperateTools.ImportData(cur);
                    cur = cur.AddDays(1);
                }
            }
            busy.Stop();
        }

        private void rbImportOneMonthData_CheckedChanged(object sender, EventArgs e)
        {
            gbEnd_ImportSchedule.Enabled = false;
        }

        private void rbImportSeveralMonthsData_CheckedChanged(object sender, EventArgs e)
        {
            gbEnd_ImportSchedule.Enabled = true;
        }

        private void btn_Schedule_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确定导入排班表数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;
            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(ImportSchedule);
            myThread.Start();
        }
        private void ImportSchedule()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            if (rbImportOneMonthSchedule.Checked)
            {
                int year = int.Parse(cbbBeginYear_ImportSchedule.SelectedItem.ToString());
                int month = int.Parse(cbbBeginMonth_ImportSchedule.SelectedItem.ToString());
                bool forceClear = cbForceClear_ImportSchedule.Checked;
                OperateTools.ImportSchedule(year, month, year, month, forceClear);
            }
            else if (rbImportSeveralMonthsSchedule.Checked)
            {
                int start_year = int.Parse(cbbBeginYear_ImportSchedule.SelectedItem.ToString()); ;
                int start_month = int.Parse(cbbBeginMonth_ImportSchedule.SelectedItem.ToString()); ;
                int end_year = int.Parse(cbbEndYear_ImportSchedule.SelectedItem.ToString()); ;
                int end_month = int.Parse(cbbEndMonth_ImportSchedule.SelectedItem.ToString()); ;
                bool forceClear = cbForceClear_ImportSchedule.Checked;

                 if (start_year > end_year || (start_year == end_year && start_month > end_month))
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                OperateTools.ImportSchedule(start_year, start_month, end_year, end_month, forceClear);
            }
            busy.Stop();
        }

        private void rbCheckOneMonthData_CheckedChanged(object sender, EventArgs e)
        {
            gbEnd_CheckSchedule.Enabled = false;
        }

        private void rbCheckSeveralMonthsData_CheckedChanged(object sender, EventArgs e)
        {
            gbEnd_CheckSchedule.Enabled = true;
        }

        private void btn_CheckSchedule_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确定检查数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;
            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(CheckSchedule);
            myThread.Start();
        }

        private void CheckSchedule()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            if (rbCheckOneMonthSchedule.Checked)
            {
                int year = int.Parse(cbbBeginYear_CheckSchedule.SelectedItem.ToString());
                int month = int.Parse(cbbBeginMonth_CheckSchedule.SelectedItem.ToString());

                OperateTools.CheckScheduleRight(year, month, year, month);
            }
            else if (rbCheckSeveralMonthsSchedule.Checked)
            {
                int start_year = int.Parse(cbbBeginYear_CheckSchedule.SelectedItem.ToString()); ;
                int start_month = int.Parse(cbbBeginMonth_CheckSchedule.SelectedItem.ToString()); ;
                int end_year = int.Parse(cbbEndYear_CheckSchedule.SelectedItem.ToString()); ;
                int end_month = int.Parse(cbbEndMonth_CheckSchedule.SelectedItem.ToString()); ;

                 if (start_year > end_year || (start_year == end_year && start_month > end_month))
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                OperateTools.CheckScheduleRight(start_year, start_month, end_year, end_month);
            }

            busy.Stop();
        }

        private void rbCheckOneDayData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_CheckForeData.Enabled = false;

        }

        private void rbCheckSeveralDaysData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_CheckForeData.Enabled = true;
        }

        private void btn_CheckRealData_Click(object sender, EventArgs e)
        {
            if (!cbCheckAutoRainReal.Checked && !cbCheckManualRainReal.Checked && !cbCheckAutoTempReal.Checked && !cbCheckManualTempReal.Checked)
            {
                MessageBox.Show("请选择需要检查的数据类型!", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (!cbCheckLackRecord_Real.Checked && !cbCheckEmptyValue_Real.Checked && !cbCheckValue_Real.Checked && !cbCheckRepeatStation_Real.Checked && !cbCheckLackStation_Real.Checked && !cbCheckAbnormalStation_Real.Checked)
            {
                MessageBox.Show("请选择检查内容!", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("请确定检查数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            //CheckRealData();

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(CheckRealData);
            myThread.Start();
        }

        private void CheckRealData()
        {

            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            DateTime beginDate = DateTime.Today, endDate = DateTime.Today;
            if (rbCheckOneDayData_Fore.Checked)
            {
                beginDate = dtpBeginDate_CheckRealData.Value.Date;
                endDate = dtpBeginDate_CheckRealData.Value.Date;
            }
            else if (rbCheckSeveralDaysData_Real.Checked)
            {
                if (dtpBeginDate_CheckRealData.Value > dtpEndDate_CheckRealData.Value)
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                beginDate = dtpBeginDate_CheckRealData.Value;
                endDate = dtpEndDate_CheckRealData.Value;
            }
            List<RealDataType> dataTypes = new List<RealDataType>();

            if (cbCheckAutoRainReal.Checked)
                dataTypes.Add(RealDataType.auto_rain);
            if (cbCheckManualRainReal.Checked)
                dataTypes.Add(RealDataType.manual_rain);
            if (cbCheckAutoTempReal.Checked)
                dataTypes.Add(RealDataType.auto_temp);
            if (cbCheckManualTempReal.Checked)
                dataTypes.Add(RealDataType.manual_temp);

            OperateTools.busy = busy;

            foreach (RealDataType dataType in dataTypes)
            {
                DataTable dt = OperateTools.CheckRealRepositoryHealth(beginDate, endDate, dataType,
                                                                      cbCheckLackRecord_Fore.Checked,
                                                                      cbCheckEmptySemi_Fore.Checked,
                                                                      cbCheckValue_Fore.Checked,
                                                                      cbCheckRepeatStation_Fore.Checked,
                                                                      cbCheckLackStation_Fore.Checked,
                                                                      cbCheckAbnormalStation_Fore.Checked);
                ShowCheckReslut(dt);

            }
            busy.Stop();

        }

        private void btn_CheckForeData_Click(object sender, EventArgs e)
        {
            bool HasCheckedItems = false;

            foreach (DataRow dr in ((DataTable)((BindingSource)dgvForeData.DataSource).DataSource).Rows)
            {
                if (dr["checked"].ToString() == "1")
                {
                    HasCheckedItems = true;
                    break; ;
                }

            }
            if (!HasCheckedItems)
            {
                MessageBox.Show("请选择需要重新入库的数据类型!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                return;
            }

            if (!cbSemiType_Auto.Checked && !cbSemiType_Manual.Checked)
            {
                MessageBox.Show("请选择评估值类型!", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (!cbFactorType_Wth.Checked && !cbFactorType_Maxt.Checked && !cbFactorType_Mint.Checked)
            {
                MessageBox.Show("请选择天气要素类型!", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (!cbCheckLackRecord_Fore.Checked && !cbCheckEmptySemi_Fore.Checked && !cbCheckValue_Fore.Checked && !cbCheckSemi_Fore.Checked && !cbCheckRepeatStation_Fore.Checked && !cbCheckLackStation_Fore.Checked && !cbCheckAbnormalStation_Fore.Checked)
            {
                MessageBox.Show("请选择检查内容!", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("请确定检查数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            //CheckForeData();

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(CheckForeData);
            myThread.Start();
            
        }

        private void CheckForeData()
        {

            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            DateTime beginDate = DateTime.Today, endDate = DateTime.Today;
            if (rbCheckOneDayData_Fore.Checked)
            {
                beginDate = dtpBeginDate_CheckForeData.Value;
                endDate = dtpBeginDate_CheckForeData.Value;
            }
            else if (rbCheckSeveralDaysData_Fore.Checked)
            {
                if (dtpBeginDate_CheckForeData.Value > dtpEndDate_CheckForeData.Value)
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                beginDate = dtpBeginDate_CheckForeData.Value;
                endDate = dtpEndDate_CheckForeData.Value;
            }
            //List< ForeDataType> dataTypes=new List<ForeDataType>();
            List<ForeSemiType> semiTypes=new List<ForeSemiType>();
            List<ForeFactorType> factorTypes = new List<ForeFactorType>();
            List<Dictionary<string, string>> dataTables = new List<Dictionary<string, string>>();

            //if (cbCheckData_CityFore.Checked)
            //    dataTypes.Add(ForeDataType.city);
            //if (cbCheckData_SiteFore.Checked)
            //    dataTypes.Add(ForeDataType.site);
            //if (cbCheckData_ZytFore.Checked)
            //    dataTypes.Add(ForeDataType.zyt);

            if (cbSemiType_Auto.Checked)
                semiTypes.Add(ForeSemiType.Auto);
            if(cbSemiType_Manual.Checked)
                semiTypes.Add(ForeSemiType.Manual);

            if (cbFactorType_Wth.Checked)
                factorTypes.Add(ForeFactorType.wth);
            if(cbFactorType_Maxt.Checked)
                factorTypes.Add(ForeFactorType.maxt);
            if(cbFactorType_Mint.Checked)
                factorTypes.Add(ForeFactorType.mint);

            foreach (DataRow dr in ((DataTable)((BindingSource)dgvForeData.DataSource).DataSource).Rows)
            {
                if (dr["checked"].ToString() == "0")
                    continue;
                //dataTables.Add(new Dictionary<string, string>());
            }

            OperateTools.busy = busy;

            //foreach (ForeDataType dataType in dataTypes)
            //    foreach (ForeSemiType semiType in semiTypes)
            //        foreach (ForeFactorType factorType in factorTypes)
            //        {
            //            DataTable dt = OperateTools.CheckForeRepositoryHealth(beginDate, endDate, dataType, semiType, factorType,
            //                                                                  cbCheckLackRecord_Fore.Checked,
            //                                                                  cbCheckEmptySemi_Fore.Checked ,
            //                                                                  cbCheckValue_Fore.Checked,
            //                                                                  cbCheckSemi_Fore.Checked,
            //                                                                  cbCheckRepeatStation_Fore.Checked,
            //                                                                  cbCheckLackStation_Fore.Checked,
            //                                                                  cbCheckAbnormalStation_Fore.Checked);
            //            ShowCheckReslut(dt);
                      
            //        }
            busy.Stop();

        }

        delegate void Callback(DataTable dt);
        public  void ShowCheckReslut(DataTable dt)
        {
            if (this.InvokeRequired)
            {
                Callback cb = new Callback(ShowCheckReslut);
                this.Invoke(cb, new object[] { dt });
                return;
            }

            if (dt == null || dt.Rows.Count == 0) return; 
            ResultForm rf = new ResultForm(dt);
            rf.Text = dt.TableName;
            rf.Show(this);
        }

        private void rbImportOneMonthManualData_CheckedChanged(object sender, EventArgs e)
        {
            gbEnd_ImportManualData.Enabled = false;
        }

        private void rbImportSeveralMonthsManualData_CheckedChanged(object sender, EventArgs e)
        {
            gbEnd_ImportManualData.Enabled = true;
        }

        private void btn_ImportManualData_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确定导入人工站数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(ImportManualData);
            myThread.Start();

        }
        private void ImportManualData()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            if (rbImportOneMonthManualData.Checked)
            {
                int year = int.Parse(cbbBeginYear_ImportManualData.SelectedItem.ToString());
                int month = int.Parse(cbbBeginMonth_ImportManualData.SelectedItem.ToString());
                bool resemi = cbReSemi_ImportManualData.Checked;
                OperateTools.ImportManualData(year, month, year, month, resemi);
            }
            else if (rbImportSeveralMonthsManualData.Checked)
            {
                int start_year = int.Parse(cbbBeginYear_ImportManualData.SelectedItem.ToString()); ;
                int start_month = int.Parse(cbbBeginMonth_ImportManualData.SelectedItem.ToString()); ;
                int end_year = int.Parse(cbbEndYear_ImportManualData.SelectedItem.ToString()); ;
                int end_month = int.Parse(cbbEndMonth_ImportManualData.SelectedItem.ToString()); ;
                bool resemi = cbReSemi_ImportManualData.Checked;

                 if (start_year > end_year || (start_year == end_year && start_month > end_month))
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                OperateTools.ImportManualData(start_year, start_month, end_year, end_month, resemi);
            }
            busy.Stop();
        }


        private void rbImportOneDayRealData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ImportRealData.Enabled = false;
        }

        private void rbImportSeveralDaysRealData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ImportRealData.Enabled = true;
        }

        private void btn_ImportRealData_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确定导入自动站数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(ImportRealData);
            myThread.Start();
        }

        private void ImportRealData()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            if (rbImportOneDayData.Checked)
                OperateTools.ImportRealData(dtpBeginDate_ImportData.Value, dtpBeginDate_ImportData.Value, cbReSemi_ImportRealData.Checked);
            else if (rbImportSeveralDaysData.Checked)
            {
                if (dtpBeginDate_ImportData.Value > dtpEndDate_ImportData.Value)
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                OperateTools.ImportRealData(dtpBeginDate_ImportData.Value, dtpEndDate_ImportData.Value, cbReSemi_ImportRealData.Checked);
            }
            busy.Stop();
        }

       

        private void rbCopyOneDayData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_CopyData.Enabled = false;
        }

        private void rbCopySeveralDaysData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_CopyData.Enabled = true;
        }

        private void btn_CopyData_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确定拷贝数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(CopyData);
            myThread.Start();
        }

        private void CopyData()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;

            if (rbCopyOneDayData.Checked)
                OperateTools.CopyDataFileForSingleDate(dtpBeginDate_ImportData.Value);
            else if (rbCopySeveralDaysData.Checked)
            {
                if (dtpBeginDate_CopyData.Value > dtpEndDate_CopyData.Value)
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                DateTime cur = dtpBeginDate_CopyData.Value;
                while (cur <= dtpEndDate_CopyData.Value)
                {
                    OperateTools.CopyDataFileForSingleDate(cur);
                    cur = cur.AddDays(1);
                }
            }

            busy.Stop();
        }


        private void rbReSemiOneDay_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ReSemi.Enabled = false;
        }

        private void rbReSemiSeveralDays_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ReSemi.Enabled = true;
        }

        private void btn_ReSemi_Click(object sender, EventArgs e)
        {
            if (!cbCityFore_ReSemi.Checked)
            {
                MessageBox.Show("请选择需要重新计算Semi的数据类型!", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("请确定重新计算Semi!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;


            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(ReSemi);
            myThread.Start();

        }
        private void ReSemi()
        {

            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            DateTime beginDate = DateTime.Today, endDate = DateTime.Today;
            if (rbReSemiOneDayData.Checked)
            {
                beginDate = dtpBeginDate_ReSemi.Value;
                endDate = dtpBeginDate_ReSemi.Value;
            }
            else if (rbReSemiSeveralDaysData.Checked)
            {
                if (dtpBeginDate_ReSemi.Value > dtpEndDate_ReSemi.Value)
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }
                beginDate = dtpBeginDate_ReSemi.Value;
                endDate = dtpEndDate_ReSemi.Value;

            }
            if (cbCityFore_ReSemi.Checked)
                OperateTools.ReCalculateSemi(dtpBeginDate_ReSemi.Value, dtpEndDate_ReSemi.Value);           

            busy.Stop();
        }

        private void rbReImportOneDayData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ReImportData.Enabled = false;
        }

        private void rbReImportSeveralDaysData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ReImportData.Enabled = true;
        }

        private void btn_ReImportData_Click(object sender, EventArgs e)
        {
          
            bool HasCheckedItems = false;

            foreach (DataRow dr in ((DataTable)((BindingSource)dgvForeData.DataSource).DataSource).Rows)
            {
                if (dr["checked"].ToString() == "1")
                {
                    HasCheckedItems = true;
                    break; ;
                }
               
            }
            if (!HasCheckedItems)
            {
                MessageBox.Show("请选择需要重新入库的数据类型!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                return;
            }

            if (MessageBox.Show("请确定重新入库数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(ReImportData);
            myThread.Start();

            //ReImportData();
        }

        private void ReImportData()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;

            DateTime beginDate = DateTime.Today, endDate = DateTime.Today;

            if (rbReImportOneDayData.Checked)
            {
                beginDate = dtpBeginDate_ReImportData.Value;
                endDate = dtpBeginDate_ReImportData.Value;
            }
            else if (rbReImportSeveralDaysData.Checked)
            {
                if (dtpBeginDate_ReImportData.Value > dtpEndDate_ReImportData.Value)
                {
                    busy.Hide();
                    MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                    busy.Stop();
                    return;
                }

                beginDate = dtpBeginDate_ReImportData.Value;
                endDate = dtpEndDate_ReImportData.Value;
            }

            if (cbForecasterFore_ReImportData.Checked)
                OperateTools.ReImportForecasterFore(beginDate, endDate);
           
            foreach (DataRow dr in ((DataTable)((BindingSource)dgvForeData.DataSource).DataSource).Rows)
            {
                if (dr["checked"].ToString() == "0")
                    continue;
               
                OperateTools.ReImportForeData(beginDate, endDate, dr["table"].ToString(), dr["dir"].ToString(), dr["filter"].ToString(), dr["description"].ToString());
            }
           

            busy.Stop();
        }

        private void btn_BuildHomeReportData_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("请确定生成首页报表数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(BuildHomeReportData);
            myThread.Start();

            //BuildHomeReportData();
        }

        private void BuildHomeReportData()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;

            List<string> kinds = new List<string>();
            if (cbThisMonth_BuildHomeReportData.Checked)
                kinds.Add("thismonth");
            if (cbLastMonth_BuildHomeReportData.Checked)
                kinds.Add("lastmonth");
            if (cbLast3Month_BuildHomeReportData.Checked)
                kinds.Add("last3month");
            if (cbThisYear_BuildHomeReportData.Checked)
                kinds.Add("thisyear");

            if (kinds.Count == 0)
            {
                busy.Hide();
                MessageBox.Show("请选择生成首页报表数据的日期!", "提示！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                busy.Stop();
                return;
            }

            OperateTools.ImportHomeReportData(kinds);

            busy.Stop();
        }

        private void rbBuildOneMonthStForeRankData_CheckedChanged(object sender, EventArgs e)
        {
            gbEnd_BuildStForeRankData.Enabled = false;
        }

        private void rbBildSeveralMonthsStForeRankData_CheckedChanged(object sender, EventArgs e)
        {
            gbEnd_BuildStForeRankData.Enabled = true;
        }

        private void btn_BuildReportData_Click(object sender, EventArgs e)
        {
            

            BuildReportData();
        }

        private void BuildReportData()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;

            int start_year = 0;
            int start_month = 0;
            int end_year = 0;
            int end_month = 0;

            if (rbBuildOneMonthData.Checked)
            {
                 start_year = int.Parse(cbbBeginYear_BuildData.SelectedItem.ToString());
                 start_month = int.Parse(cbbBeginMonth_BuildData.SelectedItem.ToString());
                 end_year = start_year;
                 end_month = start_month;
            }
            else if (rbBildSeveralMonthsData.Checked)
            {
                 start_year = int.Parse(cbbBeginYear_BuildData.SelectedItem.ToString());
                 start_month = int.Parse(cbbBeginMonth_BuildData.SelectedItem.ToString());
                 end_year = int.Parse(cbbEndYear_BuildData.SelectedItem.ToString());
                 end_month = int.Parse(cbbEndMonth_BuildData.SelectedItem.ToString());

                 if (start_year > end_year || (start_year == end_year && start_month > end_month))
                 {
                     busy.Hide();
                     MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                     busy.Stop();
                     return;
                 }

                
            }

            OperateTools.ImportForecasterRankData(start_year, start_month, end_year, end_month);

           

            busy.Stop();
        }


        private void btnSaveInfo_Click(object sender, EventArgs e)
        {
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            saveFileDialog.DefaultExt = "txt";
            if (saveFileDialog.ShowDialog() == DialogResult.Cancel) return;
            OperateTools.SaveMessage(saveFileDialog.FileName, txtLog.Text);
          
        }

        private void btn_DeleHomeReportData_Click(object sender, EventArgs e)
        {
            if (dtpBeginDate_DeleHomeReportData.Value > dtpEndDate_DeleHomeReportData.Value)
            {
                MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                return;
            }

            if (MessageBox.Show("请确定删除首页报表数据!", "提示！", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                return;

            myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(DeleHomeReportData);
            myThread.Start();
        }

        private void DeleHomeReportData()
        {
            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;

            OperateTools.DeleHomeReportData(dtpBeginDate_DeleHomeReportData.Value, dtpEndDate_DeleHomeReportData.Value);

             busy.Stop();
        }

        //private void cbZytHandinReport_CheckedChanged(object sender, EventArgs e)
        //{
        //    gbZytHandinReport.Enabled = cbZytHandinReport.Checked;
        //}

        private void rbReSemiSeveralDaysData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ReSemi.Enabled = true;
        }


        private void rbReExportOneDayData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ExportData.Enabled = false;
        }

        private void rbReExportMoreDayData_CheckedChanged(object sender, EventArgs e)
        {
            dtpEndDate_ExportData.Enabled = true;
        }

        private void btnOptimalForeData_Click(object sender, EventArgs e)
        {
            if (dtpBeginDate_ExportData.Value > dtpEndDate_ExportData.Value)
            {
                MessageBox.Show("请确保开始时间小于结束时间!", "错误！", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                return;
            }

            BuildOptimalForeData();
            //myThread.ThreadWork += new MyThread.ThreadWorkEventHandler(BuildOptimalForeData);
            //myThread.Start();
        }

        private void BuildOptimalForeData()
        {

            Busy busy = new Busy(hWnd);
            busy.Run(myThread);
            OperateTools.busy = busy;
            DateTime beginDate = DateTime.Today, endDate = DateTime.Today;
            if (rbReOptimalOneDayData.Checked)
            {
                beginDate = dtpBeginDate_ExportData.Value;
                endDate = dtpBeginDate_ExportData.Value;
            }
            else if (rbReOptimalMoreDayData.Checked)
            {
                beginDate = dtpBeginDate_ExportData.Value;
                endDate = dtpEndDate_ExportData.Value;

            }

            OperateTools.BuildOptimalForeData(beginDate, endDate);
            
            busy.Stop();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab.Name == "tabPage10")
                groupBox6.Controls.Add(dgvForeData);
            else if (tabControl1.SelectedTab.Name == "tabPage1")
                groupBox1.Controls.Add(dgvForeData);
        }

    }
}
